
ARCH                 ?= arm
DEMO_BOARD           ?= ALPHA
APP_DEMO_DIR         := app_demo
DRIVERS_DEMO_DIR     := drivers_demo

# 编译工具和编译选项
ifeq ($(ARCH), arm)
	ifeq ($(DEMO_BOARD), NT)
      KERNELDIR            ?= ~/sdk/xxx/linux-kernel
      CROSS_COMPILE_PREFIX ?= arm-ca9-linux-uclibcgnueabihf-
      ROOT_PERMISSION      :=
      export ROOT_PERMISSION
	else ifeq ($(DEMO_BOARD), ALPHA)
      KERNELDIR            ?= ~/7Linux/imx6ull-kernel
      CROSS_COMPILE_PREFIX ?= arm-linux-gnueabihf-
	endif
else
  KERNELDIR            ?= /lib/modules/$(shell uname -r)/build
  CROSS_COMPILE_PREFIX ?=
endif

# 这里不要用V，当使用V的时候，编译驱动的时候的详细信息也会打印出来
# A=1时会打印make进出子目录调用子makefile的一些信息
ifeq ("$(origin A)", "command line")
  KBUILD_VERBOSE = $(A)
endif
ifndef KBUILD_VERBOSE
  KBUILD_VERBOSE = 0
endif

ifneq ($(KBUILD_VERBOSE),1)
  # 这个参数可以屏蔽进入子目录录和出子目录的打印,会自动导出，所有子makefile都生效
  MAKEFLAGS += --no-print-directory
endif

# 导出的变量若是没有赋值，也会被传递到子makefile，并且值为空
export ARCH CROSS_COMPILE_PREFIX KERNELDIR
#=====================================================
# 编译模块和测试程序
all: app_demo.out drivers_demo_ko
	@echo "\033[1;35m""********** compile finish! **********""\033[0m"

app_demo.out:
	@echo "\033[1;35m""********** compile app_demo ... **********""\033[0m"
	$(MAKE) -C $(APP_DEMO_DIR)

drivers_demo_ko:
	@echo "\033[1;35m""********** compile drivers_demo ... **********""\033[0m"
	$(MAKE) -C $(DRIVERS_DEMO_DIR)

install: install_app install_drivers

install_app:
	$(MAKE) install -C $(APP_DEMO_DIR)

install_drivers:
	$(MAKE) install -C $(DRIVERS_DEMO_DIR)

uninstall: uninstall_app uninstall_drivers

uninstall_app:
	$(MAKE) uninstall -C $(APP_DEMO_DIR)

uninstall_drivers:
	$(MAKE) uninstall -C $(DRIVERS_DEMO_DIR)

#=====================================================
# 清理工程和调试
PHONY += FORCE
FORCE:

PHONY += clean
clean: clean_app clean_drivers
	@echo "\033[1;35m""********** make clean finish! **********""\033[0m"

clean_app:
	@echo "\033[1;35m""********** make clean app_demo ... **********""\033[0m"
	$(MAKE) clean -C $(APP_DEMO_DIR)

clean_drivers:
	@echo "\033[1;35m""********** make clean drivers ... **********""\033[0m"
	$(MAKE) clean -C $(DRIVERS_DEMO_DIR)


.PHONY: $(PHONY)

help:
	
print_app_demo_info:
	$(MAKE) print -C $(APP_DEMO_DIR)

print_drivers_demo_info:
	$(MAKE) print -C $(DRIVERS_DEMO_DIR)
